package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.AutoBean;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerDetailBean;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerFollowBean;
import com.csweimei.rentmanagementtreasure.http.entity.LookHouseBean;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopEditCustomer;
import com.csweimei.rentmanagementtreasure.pop.PopSelClick;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AutoActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.CustomerDetailActivity;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;
import com.csweimei.rentmanagementtreasure.pop.WarnPop;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;


public class CustomerInfoVm extends TitleVM {
    public ObservableField<String> customerId = new ObservableField<>();
    public ObservableField<CustomerDetailBean.DataDTO> bean = new ObservableField<>();
    public ObservableField<String> type = new ObservableField<>("0");

    public ObservableField<String> num = new ObservableField<>("0");

    public CustomerInfoVm(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("客户资料");
        setRightImg(R.mipmap.title_more);
    }

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        showPop();
    }

    @Override
    public void onResume() {
        super.onResume();
        getCustomerD();
    }

    public void getTabData(){
        if (type.get().equals("0")){
            getCustomerF();
        }else {
            getLookHouse();
        }
    }

    public BindingCommand look = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConfig.BUNDLE_KEY,bean.get());
        startActivity(CustomerDetailActivity.class,bundle);
    });

    public BindingCommand autoClick = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConfig.BUNDLE_KEY,bean.get());
        startActivity(AutoActivity.class,bundle);
    });

    private void showPop() {
        PopEditCustomer popEditCostomer = new PopEditCustomer(mActivity);
        popEditCostomer.setSelId(customerId.get());
        popEditCostomer.setCustomerBean(bean.get());
        popEditCostomer.setSelBack(new PopSelClick() {
            @Override
            public void sel(PopBean x) {
                if (x.getName().equals("DEL")){
                    showWarn();
                }
            }
        });
        PopUtils.showPop(mActivity,popEditCostomer,false);
        popEditCostomer.setShareView(shareView);
    }

    private void showWarn() {
        WarnPop warnPop = new WarnPop(mActivity, s -> delCustomer());
        PopUtils.showPop(mActivity,warnPop,false);

    }

    /**
     * 删除
     */
    private void delCustomer() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("cid",customerId.get());
        model.deletecustom(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort(o.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private void getMateInfo() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("minrent",bean.get().getMinRent());
        map.put("maxrent",bean.get().getMaxRent().replace("元","")+"");
        map.put("minarea",bean.get().getMinArea()+"");
        map.put("maxarea",bean.get().getMaxArea().replace("㎡","")+"");
        map.put("querters",bean.get().getQuarters()+"");



        model.mateinfo(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<AutoBean>() {
                    @Override
                    public void onNext(AutoBean o) {
                        if (o.issucc){
                            num.set(o.getData().size()+"");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getCustomerD() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("sid",customerId.get());
        model.customdetails(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CustomerDetailBean>() {
                    @Override
                    public void onNext(CustomerDetailBean o) {
                        if (o.issucc){
                            bean.set(o.getData());
                            getMateInfo();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<LookHouseItemVM> observableListLook = new ObservableArrayList<>();
    public ItemBinding<LookHouseItemVM> itemBindingLook = ItemBinding.of(BR.viewModel,R.layout.item_intention);


    private void getLookHouse() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("customid",customerId.get());
        model.showingslist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<LookHouseBean>() {
                    @Override
                    public void onNext(LookHouseBean o) {
                        if (o.issucc){
                            historyRecy.set(View.VISIBLE);
                            followRecy.set(View.GONE);

                            observableListLook.clear();
                            for (LookHouseBean.DataDTO bean : o.getData()) {
                                LookHouseItemVM lookHouseItemVM = new LookHouseItemVM(CustomerInfoVm.this);
                                lookHouseItemVM.setData(bean);
                                observableListLook.add(lookHouseItemVM);

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableField<Integer> followRecy = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> historyRecy = new ObservableField<>(View.GONE);

    public ObservableList<CustomerFollowItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<CustomerFollowItemVM> itemBinding = ItemBinding.of(BR.viewModel,R.layout.item_follow);

    private void getCustomerF() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("customid",customerId.get());
        model.followdetails(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CustomerFollowBean>() {
                    @Override
                    public void onNext(CustomerFollowBean o) {
                        if (o.issucc){
                            followRecy.set(View.VISIBLE);
                            historyRecy.set(View.GONE);
                            observableList.clear();
                            for (CustomerFollowBean.DataDTO data : o.getData()) {
                                CustomerFollowItemVM customerFollowItemVM = new CustomerFollowItemVM(CustomerInfoVm.this);
                                customerFollowItemVM.setData(data);
                                observableList.add(customerFollowItemVM);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public BindingCommand callClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isNotEmptyObjectOrString(bean.get()) && ValidateUtils.isNotEmptyString(bean.get().getPhone()))
                ValidateUtils.callPhone(mActivity,bean.get().getPhone());
        }
    });

    public BindingCommand msmClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isNotEmptyObjectOrString(bean.get())&& ValidateUtils.isNotEmptyString(bean.get().getPhone()))
                ValidateUtils.msnPhone(mActivity,bean.get().getPhone());
        }
    });


    ViewGroup shareView;
    public void setShareView(LinearLayout view) {
        shareView =view;
    }
}
